﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MapWindow.Map;
using MapWindow.Components;
using MapWindow.PluginInterfaces;
using MapWindow.Data;
using MapWindow.Geometries;
using LibLAS;

namespace WaterShedModel
{
    [MapWindowPlugin("WaterShed Delineation", Author = "JK", UniqueName = "WaterShed Delineation", Version = "1.0")]
    public class WaterShedModel : Extension, IMapPlugin
    {
        #region Public
        public Raster GridFile = new Raster();
        #endregion
        private ToolStripMenuItem menuItem1;
        private MapWindow.Map.IMapPluginArgs _mapWin;
        #region IMapPlugin Members
        void IMapPlugin.Initialize(IMapPluginArgs args)
        {
            _mapWin = args;
            menuItem1 = new ToolStripMenuItem("LiDAR Data Provider");
            ToolStripMenuItem menuItem2 = new ToolStripMenuItem("Load LAS Data");
            ToolStripMenuItem menuItem3 = new ToolStripMenuItem("LAS2TIN");
            ToolStripMenuItem menuItem4 = new ToolStripMenuItem("LAS2DEM");
            ToolStripMenuItem menuItem5 = new ToolStripMenuItem("LAS2SHP");
            menuItem1.DropDownItems.Add(menuItem2);
            menuItem1.DropDownItems.Add(menuItem3);
            menuItem1.DropDownItems.Add(menuItem4);
            menuItem1.DropDownItems.Add(menuItem5);
            _mapWin.MainMenu.Items.Add(menuItem1);

            menuItem2 = new ToolStripMenuItem("WaterShedModeling");
            ToolStripMenuItem menuItem6 = new ToolStripMenuItem("DEM Reconditioning");
            ToolStripMenuItem menuItem7 = new ToolStripMenuItem("Fill Sinks");
            ToolStripMenuItem menuItem8 = new ToolStripMenuItem("Flow Direction");
            ToolStripMenuItem menuItem9 = new ToolStripMenuItem("Flow Accumulation");
            ToolStripMenuItem menuItem10 = new ToolStripMenuItem("Stream Definition");
            ToolStripMenuItem menuItem11 = new ToolStripMenuItem("Stream Segmentation");
            ToolStripMenuItem menuItem12 = new ToolStripMenuItem("Catchment Grid");
            ToolStripMenuItem menuItem13 = new ToolStripMenuItem("Catchment Polygon");
            ToolStripMenuItem menuItem14 = new ToolStripMenuItem("Drainage Line");
            ToolStripMenuItem menuItem15 = new ToolStripMenuItem("Adjoint Catchment");
            ToolStripMenuItem menuItem16 = new ToolStripMenuItem("Drainage Point");
            menuItem2.DropDownItems.Add(menuItem6);
            menuItem2.DropDownItems.Add(menuItem7);
            menuItem2.DropDownItems.Add(menuItem8);
            menuItem2.DropDownItems.Add(menuItem9);
            menuItem2.DropDownItems.Add(menuItem10);
            menuItem2.DropDownItems.Add(menuItem11);
            menuItem2.DropDownItems.Add(menuItem12);
            menuItem2.DropDownItems.Add(menuItem13);
            menuItem2.DropDownItems.Add(menuItem14);
            menuItem2.DropDownItems.Add(menuItem15);
            menuItem2.DropDownItems.Add(menuItem16);
            _mapWin.MainMenu.Items.Add(menuItem2);
            //handle the menu item click event
            menuItem2.Click += new EventHandler(menuItem2_Click);
            menuItem3.Click += new EventHandler(menuItem3_Click);
            menuItem4.Click += new EventHandler(menuItem4_Click);
            menuItem5.Click += new EventHandler(menuItem5_Click);
        }
        #endregion
        private void menuItem2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("The c# menu has been Activated.");
        }
        private void menuItem3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Grid|*.asc";
            if (ofd.ShowDialog() != DialogResult.OK) return;
            GridFile.Open(ofd.FileName);
            _mapWin.Map.Layers.Add(GridFile);
            _mapWin.Map.Extents = GridFile.Bounds.Envelope;
        }
        private void menuItem4_Click(object sender, EventArgs e)
        {
            D8Computaion(GridFile);
        }
        private void D8Computaion(Raster GridFile)
        {
            MessageBox.Show("This is the D8 Direction Algorithm");
        }
        #region IExtension Members

        void IExtension.Activate()
        {
        }

        void IExtension.Deactivate()
        {
            if (_mapWin.MainMenu.Items.Contains(menuItem1))
                _mapWin.MainMenu.Items.Remove(menuItem1);
        }

        bool IExtension.IsEnabled
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion
        private void menuItem5_Click(object sender, EventArgs e)
        {
            ReadLasFile();
        }
        private void ReadLasFile()
        {
         // C# Tutorial This basic tutorial explains how to use libLAS to read and write LIDAR data encoded in LAS file format from C#.
            //try// 
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "LAS|*.las";
                if (ofd.ShowDialog() != DialogResult.OK) return;
                LASReader lasreader = new LASReader(@"E:\Lidar.las");
               // LASReader lasreader = new LASReader(ofd.FileName);
                LASPoint laspoint;
                LASHeader lasheader = lasreader.GetHeader();
                LASWriter laswriter = new LASWriter(@"E:\\Yang Ping Documents\\WaterShedModel\\SampleData\\sample_out.las", lasheader, LASReadWriteMode.LASModeWrite);
                Console.WriteLine("Number of points in file= {0}", lasheader.PointRecordsCount);
                while (lasreader.GetNextPoint())
                {
                    laspoint = lasreader.GetPoint();
                    MessageBox.Show(laspoint.X + "," + laspoint.Y + "," + laspoint.Z);
                    laswriter.WritePoint(laspoint);
                }
            }
            try
            {
            }
            catch (LASException e)
            {
                MessageBox.Show("\nLASException! Msg: {0}", e.Message);
            }
            catch
            {
                MessageBox.Show("Unknown exception caught");
            }
               finally
               {
                   MessageBox.Show("Do i need something to do?");
               }
               MessageBox.Show("End of file");
               //Console.Read();
           }
        }
}
